package com.myproject.shop.controller;


import com.myproject.shop.domain.Inventory;
import com.myproject.shop.service.IInventoryService;
import com.myproject.shop.util.AjaxResult;
import com.myproject.shop.util.BizException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/inventory")
@Api(tags = "库存管理825王慧敏")
public class InventoryController {

    @Autowired
    private IInventoryService inventoryService;

    @ApiOperation("增加库存825王慧敏")
    @ApiImplicitParam(name = "inventory", value = "包含库存编号及要增加的数量的库存对象",
            dataType = "Inventory", dataTypeClass = Inventory.class)
    @PutMapping("/increase")
    public AjaxResult increaseStock(@RequestBody Inventory inventory) {
        // 先根据库存编号查询库存是否存在
        Inventory existingInventory = inventoryService.selectInventoryById(inventory.getIid());
        if (existingInventory == null) {
            throw new BizException(500, "库存编号为 " + inventory.getIid() + " 的库存记录不存在");
        }

        // 调用服务层方法增加库存数量
        int result = inventoryService.increaseStock(inventory);
        if (result == 1) {
            return AjaxResult.success("库存增加成功");
        } else {
            return AjaxResult.error("库存增加失败");
        }
    }
}